package hsintiao.com.xintiaoAPP.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.HashMap;
import java.util.Map;

//发送短信验证码的工具类
public class SendMsg {

    private static final String INFORMATION_CHANGE = "SMS_199260015"; //信息变更
    private static final String USER_REGISTRATION = "SMS_199260017"; //用户注册
    private static final String LOGIN_CONFIRMATION = "SMS_199260019";//登录确认


    public static Map<String,String> sendSms(String phoneNum,String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G9DkzfBxt6ao2YuR6np",
                "uUKDeiQaNFKPI3mc27dzHKQhagkBUL");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNum);
        //签名信息
        request.putQueryParameter("SignName", "芯跳医疗");
        //模板信息
        request.putQueryParameter("TemplateCode", LOGIN_CONFIRMATION);
        /*//获取随机6位数验证码
        String source = "0123456789";
        Random r = new Random();
        StringBuilder rs = new StringBuilder();
        for (int j = 0; j < 6; j++) {
            rs.append(source.charAt(r.nextInt(10)));
        }
        String random = rs.toString();
        System.out.println("验证码="+random);*/
        //将验证码填充到模板中
        request.putQueryParameter("TemplateParam", "{code:"+code+"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println("=======================");
            //{"Message":"OK","RequestId":"DBD7BCB8-335F-4B48-A938-CDE02D040B4B","BizId":"950215493660290768^0","Code":"OK"}
            System.out.println(response.getData());
            return resultMsg(response.getData(),code);
        } catch (ServerException e) {
            e.printStackTrace();
            return null;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }
    //将返回的字符串转换为map集合，根据key获取状态信息
    private static Map<String,String> resultMsg(String msg,String code){
        //System.out.println(msg);
        StringBuilder builder = new StringBuilder(msg);
        String s = builder.substring(1, msg.length() - 1);
        //System.out.println(s);
        String[] split = s.split(",");
        Map result=new HashMap<>();
        for(int i=0;i<split.length;i++){
            //System.out.println(split[i]);
            String[] strSplit = split[i].split(":");
            String key=strSplit[0].substring(1,strSplit[0].length()-1);
            String value=strSplit[1].substring(1,strSplit[1].length()-1);;
            result.put(key,value);
        }
        //
        result.put("code",code);
        System.out.println("message="+result.get("Message"));
        System.out.println("RequestId="+result.get("RequestId"));
        System.out.println("Code="+result.get("Code"));
        return result;
        /*System.out.println("map集合="+result);
        System.out.println(result.get("\"Message\""));
        System.out.println(result.get("RequestId"));
        System.out.println(result.get("BizId"));
        System.out.println(result.get("Code"));*/
    }

}
